package com.xlh.dto.courseTest;

import com.google.common.collect.Lists;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.util.BeanUtil;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * @author zhr 2019/3/29
 */
@Data
public class TypeReadingDTO {
    // 题型
    private Integer type;
    // 题型中文说明
    private String typeName;
    // 题型分数
    private Integer score;
    // 题目总数
    private Integer total;
    // 题目总得分
    private Integer totalScore;
    // 正确题数
    private Integer right;
    // 错误题数
    private Integer wrong;
    // 题目情况
    private List<ReadingDTO> questionList;

    public static List<TypeReadingDTO> transform(List<ReadingDTO> readingList){
        List<TypeReadingDTO> typeReadingList = Lists.newArrayList();
        Map<Integer, List<ReadingDTO>> typeReadingListMap = BeanUtil.aggByKeyToList("type", readingList);
        typeReadingListMap.forEach((questionType, list) -> {
            TypeReadingDTO typeReading = new TypeReadingDTO();
            typeReading.setType(questionType);
            typeReading.setTypeName(QuestionTypeEnum.forCode(questionType).getMessage());
            typeReading.setQuestionList(list);

            typeReading.setTotal(list.size());
            if (QuestionTypeEnum.isObjectiveOther(questionType)) {
                typeReading.setRight((int) list.stream().filter(ReadingDTO::getRight).count());
                typeReading.setWrong(typeReading.getTotal() - typeReading.getRight());
            }

            typeReadingList.add(typeReading);
        });
        return typeReadingList;
    }

    public TypeReadingDTO convert(Integer type, List<ReadingDTO> list, Map<Integer, Integer> typeScoreMap) {
        this.setType(type);
        QuestionTypeEnum typeEnum = QuestionTypeEnum.forCode(type);
        if (typeEnum != null) {
            this.setTypeName(typeEnum.getMessage());
        }
        this.setScore(typeScoreMap.get(type));
        this.setQuestionList(list);

        this.setTotal(list.size());
        this.setTotalScore(list.stream().map(ReadingDTO::getScore).reduce(0, Integer::sum));
        if (QuestionTypeEnum.isObjectiveOther(type)) {
            this.setRight((int) list.stream().filter(ReadingDTO::getRight).count());
            this.setWrong(this.getTotal() - this.getRight());
        }
        return this;
    }
}
